package com.app.qbake.module.square;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.wind.base.adapter.BaseDelegateRecyclerAdapter;
import com.wind.base.mvp.view.BasePageFragment;
import com.wind.base.request.BaseRequest;
import com.wind.coder.annotations.Api;
import com.wind.coder.annotations.Heros;
import com.wind.coder.annotations.Param;
import com.wind.data.square.SellerGoodsListRequest;
import com.wind.data.square.SellerGoodsListResponse;
import com.app.qbake.R;
import com.app.qbake.module.square.adapter.SellerGoodsAdapter;
import com.app.qbake.module.square.di.SellerGoodsListComponent;
import com.app.qbake.module.square.event.SellerInfoEvent;
import com.app.qbake.module.square.mvp.SellerGoodsListView;
import com.app.qbake.module.square.presenter.SellerGoodsListPresenter;

import org.greenrobot.eventbus.EventBus;

import javax.inject.Inject;

@Heros(
        param = @Param(
                viewCanonicalName = "com.app.qbake.module.square.mvp.SellerGoodsListView",
                responseCanonicalName = "com.wind.data.square.SellerGoodsListResponse",
                requestCanonicalName = "com.wind.data.square.SellerGoodsListRequest",
                page = true
        ),
        api = @Api(httpMethod = Api.HttpMethod.GET, url = "user/shop")
)
public class SellerGoodsListFragment extends BasePageFragment<SellerGoodsListView,SellerGoodsListPresenter,SellerGoodsListComponent,
        SellerGoodsListResponse> implements SellerGoodsListView{
        public static final String ARGS_KEY_UID="args_key_uid";
        @Inject
        SellerGoodsListPresenter presenter;

        private String toUId;
        @Override
        public SellerGoodsListPresenter createPresenter() {
                return presenter;
        }

        @Override
        protected RecyclerView.ItemDecoration getItemDecoration() {
                return null;
        }

        @Override
        public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
                toUId=getArguments().getString(ARGS_KEY_UID);
                super.onViewCreated(view, savedInstanceState);
                mPrv.setMode(PullToRefreshBase.Mode.PULL_FROM_END);

        }

        @Override
        protected BaseDelegateRecyclerAdapter getAdapter() {
                SellerGoodsAdapter adapter=new SellerGoodsAdapter(getActivity());
                return adapter;
        }

        @Override
        protected BaseRequest buildRequest(boolean isFirstPage) {
                SellerGoodsListRequest request=new SellerGoodsListRequest();
                request.setFirstPage(isFirstPage);
                request.setType(SellerHomeTabFragment.TYPE_GOODS);
                request.setTo_userId(toUId);
                return request;
        }

        @Override
        protected int getLayoutRes() {
                return R.layout.fragment_list;
        }

        public static SellerGoodsListFragment getInstance(String toUid){
                SellerGoodsListFragment f=new SellerGoodsListFragment();
                Bundle args=new Bundle();
                args.putString(ARGS_KEY_UID,toUid);
                f.setArguments(args);
                return f;
        }

        @Override
        public void showPage(SellerGoodsListResponse response) {
                EventBus.getDefault().post(new SellerInfoEvent(response.getData()));
                super.showPage(response);

        }
}
